package com.itbaizhan.shopping_common.exception;

import com.itbaizhan.shopping_common.result.BaseResult;
import com.itbaizhan.shopping_common.result.CodeEnum;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author spf
 * @date 2023/12/12
 * @time 16:10
 */

// 统一异常处理器

@RestControllerAdvice  // 统一异常处理注解（ControllerAdvice + ResponseBody）
public class GlobalExceptionHandler {
    // 处理业务异常
    @ExceptionHandler(BusException.class)   // 异常类型
    public BaseResult defaultExceptionHandler(HttpServletRequest request, HttpServletResponse response, BusException e){
        BaseResult baseResult = new BaseResult(e.getCode(), e.getMessage(), null);
        return baseResult;
    }

    // 处理系统异常
    @ExceptionHandler(Exception.class)
    public BaseResult defaultExceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception e){
        BaseResult baseResult = new BaseResult(CodeEnum.SYSTEM_ERROR.getCode(), CodeEnum.SYSTEM_ERROR.getMessage(), null);
        return baseResult;
    }
}
